package view.subscriberpanels;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;

import prefuse.Display;
import prefuse.data.Table;
import prefuse.data.column.Column;
import prefuse.data.query.ObjectRangeModel;
import prefuse.data.query.RangeQueryBinding;
import prefuse.util.ui.JPrefuseTable;
import prefuse.util.ui.JRangeSlider;
import prefuse.visual.VisualTable;
import prefuse.visual.VisualTupleSet;
import view.ViewManager;
import view.util.ViewUtil;
import view.visualizations.DateZoomVis;
import view.visualizations.MeetZoomVis;
import view.visualizations.RaceTableVis;
import view.visualizations.XCVis;

/**
 * This is just a JComponent wrapper around the Visualization. In the future, will
 * have title and a date scrollbar probably.
 */
public class VisPane extends VisSubscriber{

	private Display display;
	private XCVis vis;
	
	public VisPane() {
		this.setMinimumSize(new Dimension(1000, 850));
	}
	
	@Override
	void showOverallView() {
		this.removeAll();
		setTitle("Cross Country Runner Data");
		showView();
		final RangeQueryBinding dateQ = vis.getDateQuery();
		vis.getXAxis().setRangeModel(dateQ.getModel());
		final JRangeSlider dateSlider = dateQ.createHorizontalRangeSlider();
		
		dateSlider.setThumbColor(null);
		dateSlider.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
            	display.setHighQuality(false);
            }
            public void mouseReleased(MouseEvent e) {
        		if (dateSlider.getLowValue() == dateSlider.getHighValue()) {
        			ObjectRangeModel model = dateQ.getObjectModel();
        			Date date = (Date) model.getObject(model.getValue());
            		ViewManager.setCurrentVis(new DateZoomVis(date));
            	}
            	display.setHighQuality(true);
            	display.repaint();
            	
            	vis.run("update");
            }
        });
		this.add(dateSlider);
	}
	
	private void setTitle(final String text) {
		JLabel titleLabel = (JLabel) ViewUtil.createLabelWithAlignment(text, CENTER_ALIGNMENT);
		titleLabel.setFont(new Font(this.getFont().getName(), Font.BOLD, this.getFont().getSize()+2));
		this.add(titleLabel);
	}
	
	void showView() {
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		
		vis = ViewManager.getCurrentVis();
		display = vis.getDisplay();
//		display.setMinimumSize(new Dimension(1000, 850));
		this.add(display);
	}

	@Override
	void showDateZoomView() {
		this.removeAll();
		this.remove(display);
		setTitle("Matches on " + ((DateZoomVis)ViewManager.getCurrentVis()).getDate());
		showView();
	}
	
	@Override
	void showMeetZoomView() {
		this.removeAll();
		vis = ViewManager.getCurrentVis();
		this.remove(display);
		setTitle(((MeetZoomVis)vis).getMeet() + " (" + ((MeetZoomVis)vis).getDate() + ")");
		showView();
		
		final RangeQueryBinding raceQ = vis.getRaceQuery();
		vis.getXAxis().setRangeModel(raceQ.getModel());
		final JRangeSlider raceSlider = raceQ.createHorizontalRangeSlider();
		raceSlider.setThumbColor(null);
		raceSlider.addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
            	display.setHighQuality(false);
            }
            public void mouseReleased(MouseEvent e) {
        		if (raceSlider.getLowValue() == raceSlider.getHighValue()) {
        			ObjectRangeModel model = raceQ.getObjectModel();
        			String race = (String) model.getObject(model.getValue());
        			MeetZoomVis mVis = (MeetZoomVis) vis;
            		ViewManager.setCurrentVis(new RaceTableVis(mVis.getDate(), mVis.getMeet(), race));
            	}
            	display.setHighQuality(true);
            	display.repaint();
            	
            	vis.run("update");
            }
        });
		this.add(raceSlider);
	}
	
	@Override
	void showTableView() {
		this.removeAll();
		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		RaceTableVis tVis = (RaceTableVis) ViewManager.getCurrentVis();
		VisualTable visTable = tVis.getVisualTable();
//		VisualTupleSet ts = tVis.getTupleSet();
		setTitle(tVis.getRace() + " (" + tVis.getMeet() + ", " + tVis.getDate() + ")");
//		this.add(Box.createVerticalStrut(20));
		
		String[] colNames = {"Place", "Name", "School", "Time", "Team Score", /*"Gender",*/ "Year"};
		List<Object[]> tableRows = new ArrayList<Object[]>();
//		System.out.println(visTable.getRowCount());
		for (int i=0; i<visTable.getRowCount(); i++) {
//			if (visTable.getChildRow(i) != -1) {
				Object[] cells = {visTable.get(i, colNames[0]), 
						visTable.get(i, colNames[1]),
						visTable.get(i, colNames[2]),
						visTable.get(i, colNames[3]),
						visTable.get(i, colNames[4]),
						visTable.get(i, colNames[5])/*,
						visTable.get(i, colNames[6])*/};
				tableRows.add(cells);
//			}
		}
		Object[][] rowArray = new Object[tableRows.size()][];
		
		tableRows.toArray(rowArray);
		JTable jTable = new JTable(rowArray, colNames);
//		jTable.setFillsViewportHeight(true);
//		jTable.setMinimumSize(new Dimension(1000, 850));
		JScrollPane tableScrollPane = new JScrollPane(jTable);
		tableScrollPane.setMinimumSize(new Dimension(1000, 400));
		
		vis = ViewManager.getCurrentVis();
		display = vis.getDisplay();
		display.setMinimumSize(new Dimension(1000, 300));
//		displa
		this.add(display);
		
//		JPanel tablePanel = new JPanel();
//		tablePanel.setMinimumSize(new Dimension(1000, 1));
		this.add(jTable.getTableHeader());
		this.add(tableScrollPane);
		
//		this.add(tablePanel);
		
		
		
//		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, display, tablePanel);
//		splitPane.setDividerLocation(500);
//		splitPane.setMinimumSize(new Dimension(1000, 1));
//		this.add(splitPane);
	}
	
	@Override
	void updateView() {
		
	}
}
